clear

*Load Data
use "./data_geo.dta", clear

*****************************************************************************
*** Setup: see Kashyap & Stein (2000) and/or Den Haan, Sumner, Yamashiro (2002) for details ***

*Drop non-states 
drop if rssd9200 == "0"
keep if  rssd9210>0 & rssd9210< 57

*Drop Delaware and/or South Dakota (see Jayaratne & Strahan 1998 JLE)
*drop if rssd9210 == 10
*drop if rssd9210 == 46

*Sample period
keep if year_q < tq(1995q1)

*Positive Assets
keep if rcfd2170 > 0

*Commercial Banks Only
keep if rssd9048 == 200 

*Insured
*keep if rssd9424 == 1 | rssd9424 == 2 | rssd9424 == 6
drop if rssd9424 == 0

*Merger
drop if MERGE_CD != .

*Set panel data
xtset rssd9001 year_q

*Fix Total Loans pre-1984 (insert before calcuating loans2 above)
gen loans2 = rcfd1400
replace loans2 = rcfd1400+rcfd2165 if year_q < tq(1984q1)

*Put loans and loan components in real terms
gen c_p_i = cpi/100
gen r_loans = loans2/c_p_i 
gen r_ci = rcfd1600/c_p_i
gen r_rlest = rcfd1410/c_p_i
gen r_con = rcfd1975/c_p_i
gen r_ag = rcfd1590/c_p_i

*Log difference 
gen loans = ln(r_loans) - ln(L.r_loans)
gen ci = ln(r_ci) - ln(L.r_ci) 
gen re = ln(r_rlest) - ln(L.r_rlest) 
gen con = ln(r_con) - ln(L.r_con)
gen ag = ln(r_ag) - ln(L.r_ag) 

*Drop outliers
sort year_q
by year_q: egen sdv = sd(loans)
drop if loans >= 5*sdv
drop if loans <= -5*sdv

*Loan Component Outliers
gen ci_share = rcfd1600/loans2
*drop if ci_share < 0.05
gen re_share = rcfd1410/loans2
*drop if re_share < 0.05
gen con_share = rcfd1975/loans2
*drop if con_share < 0.05
gen ag_share = rcfd1590/loans2
*drop if ag_share < 0.05

*Four Consecutive Quarters of Loan Growth
*ssc install tsspell
tsspell, c(loans > 0)
sort rssd9001
by rssd9001: egen maxrun = max(_seq)
drop if maxrun < 4

*Percentiles
sort year_q
by year_q: egen p95 = pctile(rcfd2170), p(95)
by year_q: egen p99 = pctile(rcfd2170), p(99)

*Generate Quarter/Year/timetrend Series
sort rssd9001 year_q
gen Quarter = quarter(date2)
gen Year = year(date2) 
egen time = group(year_q)

*Choose to keep (or drop) year deregulation was implemented 
replace intra = 1 if intra == .
replace inter =1 if inter == .

* Generate Interaction Terms
gen rr_intra = rr*intra
gen rr_intra1 = L1.rr*intra
gen rr_intra2 = L2.rr*intra
gen rr_intra3 = L3.rr*intra
gen rr_intra4 = L4.rr*intra

gen rr_inter = rr*inter
gen rr_inter1 = L1.rr*inter
gen rr_inter2 = L2.rr*inter
gen rr_inter3 = L3.rr*inter
gen rr_inter4 = L4.rr*inter

*******************************************************************************
*** Bank-level Characteristics ***

* BHC Status
gen bhc = 0
replace bhc = 1 if rssd9379 != 0

* Size (total assets)
gen Size = ln(rcfd2170)

* Liquidity ratio
gen Liq = rcfd0010/rcfd2950

*Equity raito
gen Cap = rcfd3210/rcfd2170

* Securities
gen Sec = rcfd0390 + rcfd1350
gen sec84 = rcfd0400 + rcfd0600 + rcfd0900 + rcfd0380 + rcfd1350
replace Sec = sec84 if year_q < tq(1984q1) 


*******************************************************************************
********************************************************************************
*** BHC Status ***

gen prebhc = 0
replace prebhc = 1 if bhc==1 & inter==0
gen postbhc = 0
replace postbhc = 1 if bhc==1 & inter==1 

sort rssd9001
by rssd9001: egen affiliated = max(bhc)
by rssd9001: egen pre_bhc = max(prebhc)
by rssd9001: egen post_bhc = max(postbhc)
sort rssd9001 year_q

*******************************************************************************
*** BHC Variables ***
gen ones = 1
sort rssd9379 year_q
by rssd9379 year_q: egen hc_banks = sum(ones)
by rssd9379 year_q: egen hc_assets = sum(rcfd2170)
by rssd9379 year_q: egen hc_loans = sum(loans2)
by rssd9379 year_q: egen hc_cap = sum(rcfd3210)
by rssd9379 year_q: egen hc_dep = sum(rcfd2200)
replace hc_banks = 0 if bhc == 0
replace hc_assets = 0 if bhc == 0
replace hc_loans = 0 if bhc == 0
replace hc_cap = 0 if bhc == 0
replace hc_dep = 0 if bhc == 0
gen lnhc_assets = log(hc_assets)
replace lnhc_assets = 0 if lnhc_assets == .
gen hc_eq = log(hc_cap)
replace hc_eq = 0 if hc_eq == .


*** Extend BHC Distance post-1994 ***
replace bhc_distance = 0 if bhc_distance==. & Year <= 1994
*sum bhc_distance
*replace bhc_distance = bhc_distance[_n-1] if bhc_distance==. & Year > 1994
*sum bhc_distance

sort rssd9001 year_q
gen assetr = rcfd2170/hc_assets
gen loansr = loans2/hc_loans
gen capr = rcfd3210/hc_cap
gen depr = rcfd2200/hc_dep
replace assetr = 1 if bhc == 0
replace loansr = 1 if bhc == 0
replace capr = 1 if bhc == 0
replace depr = 1 if bhc == 0

sort rssd9001 year_q

keep if rcfd2170 < p95 & pre_bhc == 1

********************************************************************************
*** Column 1 ***
reghdfe hc_eq L(0/4).inter pi_pchg hpi_chg, absorb(rssd9001 year_q) cluster(rssd9210)
lincom inter + L1.inter + L2.inter + L3.inter + L4.inter

*** Columns 2-3 ***
egen m50 = pctile(hc_cap), p(50)
gen size = 0
replace size = 1 if hc_cap > m50 

gen size_intra = intra*size
gen size_inter = inter*size

gen rr_size = rr*size
gen rr_size1 = L1.rr*size
gen rr_size2 = L2.rr*size
gen rr_size3 = L3.rr*size
gen rr_size4 = L4.rr*size

gen rr_intra_size = rr*intra*size
gen rr_intra_size1 = L1.rr*intra*size
gen rr_intra_size2 = L2.rr*intra*size
gen rr_intra_size3 = L3.rr*intra*size
gen rr_intra_size4 = L4.rr*intra*size

gen rr_inter_size = rr*inter*size
gen rr_inter_size1 = L1.rr*inter*size
gen rr_inter_size2 = L2.rr*inter*size
gen rr_inter_size3 = L3.rr*inter*size
gen rr_inter_size4 = L4.rr*inter*size 

gen ln_size1 = L1.loans*size
gen ln_size2 = L2.loans*size
gen ln_size3 = L3.loans*size
gen ln_size4 = L4.loans*size

gen pi_size = pi_pchg*size
gen pi_size1 = L1.pi_pchg*size
gen pi_size2 = L2.pi_pchg*size
gen pi_size3 = L3.pi_pchg*size
gen pi_size4 = L4.pi_pchg*size

gen hpi_size = hpi_chg*size
gen hpi_size1 = L1.hpi_chg*size
gen hpi_size2 = L2.hpi_chg*size
gen hpi_size3 = L3.hpi_chg*size
gen hpi_size4 = L4.hpi_chg*size

egen size_yearq = group(size year_q)

reghdfe loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg inter rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 size size_inter rr_size rr_size1 rr_size2 rr_size3 rr_size4 rr_inter_size rr_inter_size1 rr_inter_size2 rr_inter_size3 rr_inter_size4 ln_size1 ln_size2 ln_size3 ln_size4 pi_size pi_size1 pi_size2 pi_size3 pi_size4 hpi_size hpi_size1 hpi_size2 hpi_size3 hpi_size4 if rcfd2170 < p95 & pre_bhc == 1, absorb(rssd9001 year_q) cluster(rssd9210) 
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4
lincom rr_size + rr_size1 + rr_size2 + rr_size3 + rr_size4
lincom rr_inter_size + rr_inter_size1 + rr_inter_size2 + rr_inter_size3 + rr_inter_size4

reghdfe loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg inter rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 size size_inter rr_size rr_size1 rr_size2 rr_size3 rr_size4 rr_inter_size rr_inter_size1 rr_inter_size2 rr_inter_size3 rr_inter_size4 ln_size1 ln_size2 ln_size3 ln_size4 pi_size pi_size1 pi_size2 pi_size3 pi_size4 hpi_size hpi_size1 hpi_size2 hpi_size3 hpi_size4 if rcfd2170 < p95 & pre_bhc == 1, absorb(rssd9001 year_q size_yearq) cluster(rssd9210) 
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4
lincom rr_size + rr_size1 + rr_size2 + rr_size3 + rr_size4
lincom rr_inter_size + rr_inter_size1 + rr_inter_size2 + rr_inter_size3 + rr_inter_size4


*** Columns 4-5 ***
gen rr_cap = rr*hc_eq
gen rr_cap1 = L1.rr*hc_eq
gen rr_cap2 = L2.rr*hc_eq
gen rr_cap3 = L3.rr*hc_eq
gen rr_cap4 = L4.rr*hc_eq

ivreg2 loans L(1/4).loans L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx L(0/4).rr (hc_eq rr_cap rr_cap1 rr_cap2 rr_cap3 rr_cap4 = inter rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4) i.Quarter i.Year, cluster(rssd9210) first
lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
lincom rr_cap + rr_cap1 + rr_cap2 + rr_cap3 + rr_cap4

ivreg2 loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg (hc_eq rr_cap rr_cap1 rr_cap2 rr_cap3 rr_cap4 = inter rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4) i.year_q, cluster(rssd9210) first
lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
lincom rr_cap + rr_cap1 + rr_cap2 + rr_cap3 + rr_cap4
********************************************************************************